package com.gzx.bi.modular.screen.util;

import com.gzx.bi.modular.screen.vo.GzxLocalDateTimeVo;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 大屏日期工具类
 */
public class ScreenDateUtils {

    // 获取上个月的今天
    public static String getLastMonthDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);

        // 处理跨月天数问题
        int maxDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        if (calendar.get(Calendar.DAY_OF_MONTH) > maxDay) {
            calendar.set(Calendar.DAY_OF_MONTH, maxDay);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }

    //获取昨天的日期
    public static String getYesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }

    //获取当月
    public static String currentMonth() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        return sdf.format(calendar.getTime());
    }

    //获取今天的日期
    public static String getMonthDay() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime());
    }

    //获取本月1号到当天的日期
    public static List<String> getMonthDayList() {
        List<String> dateList = new ArrayList<>();
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 获取本月1号
        LocalDate firstDayOfMonth = today.withDayOfMonth(1);

        // 生成日期列表
        List<LocalDate> datesInMonth = new ArrayList<>();
        LocalDate currentDate = firstDayOfMonth;

        while (!currentDate.isAfter(today)) {
            datesInMonth.add(currentDate);
            currentDate = currentDate.plusDays(1);
        }

        // 格式化输出
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        datesInMonth.forEach(date -> dateList.add(date.format(formatter)));
        // 获取总天数
        return dateList;
    }

    //获取今天0点0分0秒
    public static LocalDateTime getNowZeroTime() {
        return LocalDate.now().atStartOfDay();
    }

    //获取今天23点59分59秒
    public static LocalDateTime getNowLastTime() {
        return LocalDate.now().atTime(23, 59, 59);
    }

    //获取当月1号到今天的0点0分0秒的时间和23点59分59秒的时间
    public static List<GzxLocalDateTimeVo> getMonthListDate() {
        List<GzxLocalDateTimeVo> dateList = new ArrayList<>();
        for (String s : getMonthDayList()) {
            GzxLocalDateTimeVo gzxLocalDateTimeVo = new GzxLocalDateTimeVo();
            gzxLocalDateTimeVo.setStartDate(LocalDateTime.parse(s + " 00:00:00", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            gzxLocalDateTimeVo.setEndDate(LocalDateTime.parse(s + " 23:59:59", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            dateList.add(gzxLocalDateTimeVo);
        }
        return dateList;
    }

    //获取当天0点0分0秒的时间和23点59分59秒的时间
    public static GzxLocalDateTimeVo getNowDate() {
        return new GzxLocalDateTimeVo(getNowZeroTime(), getNowLastTime());
    }

    //截取日期前10位
    public static String getDate(LocalDateTime date) {
        if (ObjectUtils.isEmpty(date)) {
            return "";
        }
        return date.toString().substring(0, 10);
    }

    public static void main(String[] args) {
        // 示例：RestTemplate调用
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("audio_file", new FileSystemResource("C:\\Users\\Baolo\\Downloads\\1.wav"));

        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(body, headers);
        String response = restTemplate.postForObject("http://117.72.49.18:10096/asr", request, String.class);
        System.out.printf("rsp -> ", response);
    }

}
